<!DOCTYPE html>
<html class="no-js">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title></title>
        <meta name="description" content="" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" href="" />
    </head>
  
    <body>
        <input type="file" id="myFile" />
        <a id="test">13</a>
        <script type="text/javascript">
            window.onload=function(){
                console.log(1111)
            }
            if (window.FileReader) {
                // java.net.URL url = TestScriptEngine.class.getClassLoader().getResource("a.js");
                // //System.out.println(url.getPath().substring(1).replace("%20", " "));
                // FileReader fileReader = new FileReader(url.getPath());
                var reader = new FileReader();
            } else {
                console.log('你的浏览器不支持读取文件');
            }
            var myFile = document.querySelector('#myFile');
            var a=document.querySelector('#test');
            myFile.onclick=function(){
                console.log('click')
            }
            a.click();
            myFile.onchange = function () {
                var file = myFile.files[0];
                reader.readAsDataURL(file);
                reader.onload = function () {
                    var data = reader.result;   //base64形式的文件内容
                    // const img=new Image();
                    // img.src=data;
                    // document.body.appendChild(img)
                };
                reader.onerror = function(){
                    console.log('读取失败');
                    console.log(reader.error);
                }
            };
            console.log(123)

            function formatToFileNoType(ret,fileName){
                const blob = new Blob([ret.data], {
                    type:'application/octet-stream',
                });//先将获取到的数据流转成Blob数据
                const reader = new FileReader();//通过fileReader转成file
                reader.readAsDataURL(blob);//通过fileReader转成file
                reader.onload = e => {
                    const a = document.createElement('a');//创建页面链接元素
                    if(fileName) {
                        // 前端命名文件
                        a.download = fileName
                    }else {
                        // 后端命名文件
                        a.download = decodeURIComponent(ret.headers['content-disposition'].split('filename=')[1])
                    }
                    a.href = e.target.result;//设置下载链接
                    document.body.appendChild(a);//把元素插入body中
                    a.click();//模拟点击
                    document.body.removeChild(a);//把元素去除。
                };
            }


        </script>
    </body>
</html>